package net.dipper.tankwar.editor.ui;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import net.dipper.tankwar.core.data.ObjectMap;
import net.dipper.tankwar.core.helper.Position;
import net.dipper.tankwar.core.helper.ViewPort;
import net.dipper.tankwar.core.config.ConfigItem;
import net.dipper.tankwar.core.config.ConfigManager;
import net.dipper.tankwar.editor.helper.ObjectRenderer;
import net.dipper.tankwar.editor.helper.ObjectRendererFactory;

import wnb.util.io.ImageManager;
import wnb.util.ui.Animation;

public class EditorAnimation implements Animation {
	private EditorContext context;
	
	public EditorAnimation(EditorContext context){
		this.context = context;
	}

	@Override
	public void paintFrame(Graphics2D g2, int width, int height, int frameIndex) {
		try {
			drawBackground(g2, width, height);
			drawObject(g2, width, height);
			drawSelect(g2, width, height);
			drawPlayer(g2, width, height, frameIndex);
		} catch (ClassNotFoundException | InstantiationException
				| IllegalAccessException | ParserConfigurationException
				| SAXException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void drawPlayer(Graphics2D g2, int width, int height, int frameIndex) {
		ViewPort viewPort = context.getViewPort();
		Position player = context.getMap().getPlayer();
		
		int value = frameIndex % 4;
		switch(value){
		case 0:
			g2.setPaint(new Color(255, 255, 0, 0));
			break;
			
		case 1:
			g2.setPaint(new Color(255, 255, 0, 63));
			break;
			
		case 2:
			g2.setPaint(new Color(255, 255, 0, 127));
			break;
			
		case 3:
			g2.setPaint(new Color(255, 255, 0, 255));
			break;
		}
		
		int gW = width / viewPort.width;
		int gH = height / viewPort.height;
		int x = player.x - viewPort.x;
		int y = player.y - viewPort.y;
		g2.fillRect(x * gW, y * gH, gW, gH);
	}

	private void drawObject(Graphics2D g2, int width, int height) throws ClassNotFoundException, InstantiationException, IllegalAccessException, ParserConfigurationException, SAXException, IOException {
		ConfigManager manager = ConfigManager.getInstance();
		ViewPort viewPort = context.getViewPort();
		ObjectMap map = context.getMap();
		int gW = width / viewPort.width;
		int gH = height / viewPort.height;
		for(int i=viewPort.x;i<viewPort.width + viewPort.x;i++){
			for(int j=viewPort.y;j<viewPort.height + viewPort.y;j++){
				byte value = map.getObject(i , j );
				ConfigItem item = manager.getObject(value);
				if(item == null)
					continue;
				
				Image img = ImageManager.getImage(item.getImg());
				g2.drawImage(img, (i - viewPort.x) * gW, (j - viewPort.y) * gH, gW, gH, null);
			}
		}
	}

	private void drawSelect(Graphics2D g2, int width, int height) {
		ViewPort viewPort = context.getViewPort();
		int gW = width / viewPort.width;
		int gH = height / viewPort.height;
		Point d = context.getCursor();
		Point s = context.getSelected();
		
		//编辑模式
		if(context.getMode() == EditorContext.MODE_EDIT && context.getCurrentItem() != null){
			ObjectRenderer renderer = ObjectRendererFactory.newInstance(context.getCurrentItem());
			renderer.renderSelection(g2, d.x, d.y, gW, gH);
		}else if(context.getMode() == EditorContext.MODE_DISPLAY){
			g2.setColor(Color.ORANGE);
			g2.drawRect(s.x * gW, s.y * gH, gW, gH);
		}
		
		g2.setPaint(new Color(255, 180, 180, 120));
		g2.fillRect(d.x * gW, d.y * gH, gW, gH);
		g2.setPaint(null);
	}

	//绘制背景层
	private void drawBackground(Graphics2D g2, int width, int height) throws ClassNotFoundException, InstantiationException, IllegalAccessException, ParserConfigurationException, SAXException, IOException {
		ConfigManager manager = ConfigManager.getInstance();
		ViewPort viewPort = context.getViewPort();
		ObjectMap map = context.getMap();
		int gW = width / viewPort.width;
		int gH = height / viewPort.height;
		for(int i=viewPort.x;i<viewPort.width + viewPort.x;i++){
			for(int j=viewPort.y;j<viewPort.height + viewPort.y;j++){
				byte value = map.getBackground(i, j);
				ConfigItem item = manager.getBackground(value);
				if(item == null)
					continue;
				
				Image img = ImageManager.getImage(item.getImg());
				g2.drawImage(img, (i - viewPort.x) * gW, (j - viewPort.y) * gH, gW, gH, null);
			}
		}
	}

	@Override
	public int getFrameCount() {
		return 0;
	}

}
